using System.ComponentModel;
using DA.Client.Base.Abstractions.Navigation;
using DA.Client.Base.Helpers;
using DA.Client.Base.Models.Navigation;
using DA.Client.Base.ViewModels;

namespace DA.Client.Base.Impl.Navigation;

public class DefaultViewNavigator(IEnumerable<INavigationCommand> commands) : IViewNavigator
{
    /// <summary>
    /// 当前页视图模型
    /// </summary>
    public PageViewModelBase? CurrentPage { get; private set; }

    /// <summary>
    /// 属性变化通知事件
    /// </summary>
    public event PropertyChangedEventHandler? PropertyChanged;

    /// <summary>
    /// 导航命令集合
    /// </summary>
    private readonly IEnumerable<INavigationCommand> _commands = commands;

    /// <summary>
    /// 设置当前页面
    /// </summary>
    /// <param name="page"></param>
    public void SetCurrentPage(PageViewModelBase page)
    {
        CurrentPage = page;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentPage)));
    }

    /// <summary>
    /// 导航到指定视图页面
    /// </summary>
    /// <param name="key"></param>
    /// <exception cref="ArgumentOutOfRangeException"></exception>
    public void NavigateTo(string key)
    {
        ArgumentExceptionHelper.ThrowIfNullEmpty(key);

        var command = _commands.FirstOrDefault(c => c.Key == key);

        ArgumentExceptionHelper.ThrowIfNavigationCommandNull(command);

        command?.Execute(new NavigationCommandContext(this));
    }
}